<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Ajax extends CI_Controller {
	function __construct(){
		parent::__construct();
		$this->load->model('Ajax_model');
		$this->user = $this->session->userdata('user');
		$this->comment_key = $this->config->item('encryption_key');
	}
	
	function get_comments(){
		$data = array();
		$param = $this->input->post('param', true);
		$key = $this->input->post('key', true);
		if ($key == md5('lishuzulishuzu')) {
			$id = intval($param['id']);
			$idtype = xss_clean($param['idtype']);
			if($id && $idtype){
				$data['list'] = $this->Ajax_model->get_comments($id, $idtype);
				$data['id'] = $id;
				$data['idtype'] = $idtype;
				$data['comment_key'] = md5($id.$idtype.$this->comment_key);//debug
				$data['uid'] = $this->user['uid'];
			}
		}
		
		$data['userinfo'] = $this->user;
		$this->load->template('comment_tpl', $data);
	}
	
	function add_comment(){
		$data = array();
		$param = $this->input->post('param', true);
		$message = $param['message'] ? $param['message'] : '';
		$id = $param['id'] ? intval($param['id']) : '';
		$idtype = xss_clean($param['idtype']);
		$key = $this->input->post('key', true);
		if (!$this->user['uid']) {
			showjsonmessage('您还没有登录,不能评论', '2');
		}
		if ($key == md5($id.$idtype.$this->comment_key)) {
			if($id && $idtype && $message){
					$cid = $this->Ajax_model->add_comment($id, $idtype, $message);
				if ($cid) {
					showjsonmessage('操作成功', '1');
				}else{
					showjsonmessage('操作失败', '3');
				}
			}
		}
		showjsonmessage('非法请求', '4');
	}
	
	function del_comment(){
		$data = array();
		$param = $this->input->post('param', true);
		$id = $param['id'] ? intval($param['id']) : '';
		$idtype = xss_clean($param['idtype']);
		$cid = $param['cid'] ? intval($param['cid']) : '';
		$key = $this->input->post('key', true);
		if (!$this->user['uid']) {
			showjsonmessage('您还没有登录,不能删除评论', '2');
		}
		if ($key == md5($id.$idtype.$this->comment_key)) {
			if($id && $idtype && $cid){
				$res = $this->Ajax_model->del_comment($cid);
				if ($res == '1') {
					showjsonmessage('操作成功', '1');
				}elseif($res == '2'){
					showjsonmessage('不能删除他人评论', '2');
				}else{
					showjsonmessage('操作失败', '3');					
				}
			}
		}
		showjsonmessage('非法请求', '4');
	}
	// 喜欢
	function like(){
		$data = array();
		$param = $this->input->post('param', true);
		$wid = $param['wid'] ? intval($param['wid']) : '';
		$key = $this->input->post('key', true);
		if (!$this->user['uid']) {
			showjsonmessage('您还没有登录,不能收藏', '2');
		}
		if ($key == get_workkey($param['wid'])) {
			if(intval($param['wid'])){
				$collection = array(
						'wid' => $wid,
						'uid' => $this->user['uid'],
						'name' => $this->user['name'],
						'dateline' => now(),
					);
				$result = $this->Ajax_model->add_collection($collection);
				if ($result['code'] == '1') {
					$workinfo = $this->work_model->get_work('wid', $wid);
					//关注提醒
					$note = $this->lang->line('note_like')."<a href='".base_url().'work/index/'.$wid."'>".cutstr($workinfo['title'], 75)."</a>";
					$this->notification_model->notification_add($workinfo['uid'], 'like', $note);
					
					showjsonmessage('操作成功', '1');
				}elseif ($result['code'] == '2') {
					showjsonmessage('已经收藏过', '5');					
				}else{
					showjsonmessage('操作失败', '3');					
				}
			}
		}
		showjsonmessage('非法请求', '4');
	}
	// 取消喜欢
	function unlike(){
		$data = array();
		$param = $this->input->post('param', true);
		$wid = $param['wid'] ? intval($param['wid']) : '';
		$key = $this->input->post('key', true);
		if (!$this->user['uid']) {
			showjsonmessage('您还没有登录,不能取消收藏', '2');
		}
		if ($key == get_workkey($param['wid'])) {
			if(intval($param['wid'])){
				$result = $this->Ajax_model->del_collection($wid);
				if ($result['code'] == '1') {
					showjsonmessage('操作成功', '1');
				}elseif ($result['code'] == '2') {
					showjsonmessage('未登录', '2');					
				}else{
					showjsonmessage('操作失败', $result['code']);
				}
			}
		}
		showjsonmessage('非法请求', '4');
	}
	
	function follow(){
		$data = array();
		$param = $this->input->post('param', true);
		$uid = $param['uid'] ? intval($param['uid']) : '';
		$key = $this->input->post('key', true);
		if (!$this->user['uid']) {
			showjsonmessage('您还没有登录,不能关注别人', '2');
		}
		if(intval($uid)){
			$ship = array(
					'uid' => $this->user['uid'],
					'followid' => $uid,
					'dateline' => now(),
				);
				
			$result = $this->Ajax_model->follow($ship);
			if ($result['code'] == '1') {
				//关注提醒
				$note = $this->lang->line('note_follow');
				$this->notification_model->notification_add($uid, 'ship', $note);
				showjsonmessage('操作成功', '1');
			}elseif ($result['code'] == '2') {
				showjsonmessage('已经关注', '5');					
			}else{
				showjsonmessage('操作失败', '3');					
			}
		}
		showjsonmessage('非法请求', '4');
	}
	
	function unfollow(){
		$data = array();
		$param = $this->input->post('param', true);
		$uid = $param['uid'] ? intval($param['uid']) : '';
		$key = $this->input->post('key', true);
		if (!$this->user['uid']) {
			showjsonmessage('您还没有登录,不能关注别人', '2');
		}
		if(intval($uid)){
			$ship = array(
					'uid' => $this->user['uid'],
					'followid' => $uid,
				);
			$result = $this->Ajax_model->unfollow($ship);
			if ($result['code'] == '1') {
				showjsonmessage('操作成功', '1');
			}elseif ($result['code'] == '2') {
				showjsonmessage('未登录', '2');					
			}else{
				showjsonmessage('操作失败', '3');					
			}
		}
		showjsonmessage('非法请求', '4');
	}
	
	function del_pm(){
		$param = $this->input->post('param', true);
		$touid = $param['touid'] ? intval($param['touid']) : '';
		if (!$this->user['uid']) {
			showjsonmessage('您还没有登录,不能删除消息', '2');
		}
		if (!$touid) {
			showjsonmessage('无法删除指定的信息', '3');
		}

		if ($touid && $this->user['uid']) {
			$this->load->model('pm_model');
			$remove = $this->pm_model->deleteuidpm($this->user['uid'], array($touid));
			if ($remove) {
				showjsonmessage('删除成功', '1');
			}else{
				showjsonmessage('删除失败', '4');
			}
		}
		showjsonmessage('操作失败', '4');
	}
}